package com.blackman.skcrawler.data.service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.blackman.skcrawler.core.crawler.api.IDefaultDataCallBack;
import com.blackman.skcrawler.core.crawler.po.WormGrabedPage;
import com.blackman.skcrawler.core.crawler.po.WormHomePage;
import com.blackman.skcrawler.core.crawler.po.WormPendPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DefaultDataCallBackImpl implements IDefaultDataCallBack {

    @Autowired
    private WormGrabedPageServiceImpl wormGrabedPageService;

    @Autowired
    private WormHomePageServiceImpl wormHomePageService;

    @Autowired
    private WormPendPageServiceImpl wormPendPageService;

    @Autowired
    private WormPageRegularServiceImpl wormPageRegularService;


    /**
     * 获取起始页列表数据
     * @param homePage 抓取入口
     * @return
     */
    public List<WormHomePage> findHomePageList(WormHomePage homePage) {
        return wormHomePageService.selectListByEntity(homePage);
    }

    /**
     * 更新或新增起始页
     *
     * 更新：根据地址为标识判断
     *
     * @param urlList 链接地址
     */
    public void upHomePage(List<String> urlList) {
        wormHomePageService.addOrUpdate(urlList);
    }

    /**
     * 更新或新增 正则匹配规则
     *
     * 更新：正则+匹配类型判断
     *
     * @param regularList 正则列表
     * @param regularType 匹配类型
     */
    public void upPageRegular(List<String> regularList, String regularType) {
        wormPageRegularService.addOrUpdate(regularList, regularType);
    }

    /**
     * 寻找匹配种子正则规则列表
     * @param type 类型
     */
    public List<String> findSeedListByType(String type) {
        return wormPageRegularService.findTypeList(type);
    }

    /**
     * 获取待抓取url
     * @param count 获取待抓取列表数
     * @return
     */
    public List<WormPendPage> findPendPageList(int count) {
        Page<WormPendPage> page = new Page<WormPendPage>();
        page.setSize(100);
        EntityWrapper<WormPendPage> pendPageEntityWrapper = new EntityWrapper<WormPendPage>();
        pendPageEntityWrapper.eq("status", WormPendPage.STATUS_NORMAL);
        pendPageEntityWrapper.eq("enabled", false);
        pendPageEntityWrapper.orderBy("modify_time", true);
        page = wormPendPageService.selectPage(page, pendPageEntityWrapper);
        if(page != null && page.getRecords() != null){
            for(int i = 0;i < page.getRecords().size();i++){
                page.getRecords().get(i).setQueueTime(new Date());
                page.getRecords().get(i).setStatus(WormPendPage.STATUS_RUNING);
                page.getRecords().get(i).setModifyTime(new Date());
            }
            if(page.getRecords().size() != 0){
                wormPendPageService.updateBatchById(page.getRecords());
            }else{
                // 已经没有待抓取的列表数据了

            }
        }
        return page.getRecords();
    }

    /**
     * 更新待抓取数据
     * @param pendPageList 待抓取数据
     */
    public void upPendPageBatchById(List<WormPendPage> pendPageList) {
        wormPendPageService.updateBatchById(pendPageList);
    }

    /**
     * 更新待抓取数据
     */
    public void upPendPage(WormPendPage pendPage) {
        pendPage.setModifyTime(new Date());
        wormPendPageService.updateById(pendPage);
    }

    /**
     * 根据状态获取待抓取数据量
     * @param status 状态
     * @return
     */
    public int selectPendPageCountByStatus(String status) {
        WormPendPage pendPage = new WormPendPage();
        pendPage.setStatus(status);
        int count = wormPendPageService.selectCountByEntity(pendPage);
        return count;
    }

    /**
     * 根据url获取已抓取数据
     * @param url 链接地址
     * @return
     */
    public WormGrabedPage selectOneByUrl(String url) {
        WormGrabedPage grabedPage = new WormGrabedPage();
        grabedPage.setPageUrl(url);
        grabedPage = wormGrabedPageService.selectOneByEntity(grabedPage);
        return grabedPage;
    }

    /**
     * 更新已抓取数据
     * @param grabedPage 已抓取
     */
    public void upGrabedPageById(WormGrabedPage grabedPage) {
        grabedPage.setModifyTime(new Date());
        wormGrabedPageService.updateById(grabedPage);
    }

    /**
     * 新增已抓取数据
     * @param grabedPage 已抓取
     */
    public void insertGrabedPage(WormGrabedPage grabedPage) {
        grabedPage.setCreateTime(new Date());
        grabedPage.setModifyTime(new Date());
        wormGrabedPageService.insert(grabedPage);
    }
}
